<?php

/**
 * Description of OrderSyncer
 *
 * @author Inori
 */
class OrderInvoicePost extends Syncer {

  protected function getRequestType() {
	return 'invoice';
  }

  protected function getWebServiceType() {
	return SimpleWebService::DIRECTO_INPUT;
  }

  protected function mapData(array $pks) {
	$q = Doctrine_Query::create()
					->select('po.*, pu.*, poi.*, pos.*')
					->from('ProductOrder po')
					->innerJoin('po.OrederedItems poi')
					//->innerJoin('poi.Product p')
					->innerJoin('po.PublicUser pu')
					->innerJoin('po.Shippings pos')
//			->leftJoin('p.Translation pt WITH pt.lang = ?', 'et')
//			->leftJoin('p.ProductGroups pg')
					->whereIn('po.id', $pks)
//			->groupBy('po.id')
					->setHydrationMode(Doctrine::HYDRATE_ARRAY);

	$result = $q->execute();
	return $result;
  }

  public function getXmlStr(array $items) {
	sfProjectConfiguration::getActive()->loadHelpers(array('I18N', 'Variable'));
	$xmlStr = '';
	foreach ($items as $item) {
	  $class = $item['PublicUser']['temp'] == 1 ? 'WEBGUEST' : 'WEBREG';
	  $name = $item['PublicUser']['temp'] == 1 ? 'Külastaja' : $item['PublicUser']['first_name'] . ' ' . $item['PublicUser']['last_name'];
	  $xmlStr .= '<order
				  WebID="' . $item['order_number'] . '"
				  Date="' . $item['created_at'] . '"
				  CustomerName="' . $name . '"
				  CustomerCode="' . $item['PublicUser']['id'] . '"
				  CustomerClass="' . $class . '"
				  Address1="' . $item['Shippings'][0]['information'] . '"
				  Email="' . $item['PublicUser']['email'] . '"
				  StockCode="PL"
				  PaymentTerm="KM">';
	  $price = '';
	  foreach ($item['OrederedItems'] as $prod) {
		$price += $prod['price'] * $prod['quanity'];
		$prodPrice = round($prod['price'] * $prod['quanity']/1.2,2);
		$disc1 = 0;$disc2 = 0;
		if ($item['reg_discount'] != 0) {
		  $disc1 = round($prodPrice - ($prodPrice * (1 - $item['reg_discount'] / 100)),2);
		}
		if ($item['discount_coup'] != 0) {
		  $disc2 = round($prodPrice - ($prodPrice * (1 - $item['discount_coup'] / 100)),2);

		}		
		
		$xmlStr .= '<line
					ProductID="' . $prod['code'] . '"
					Quantity="' . $prod['quanity'] . '"
					UnitPrice="' . $prodPrice . '"
					LineTotal="' . ($prodPrice - $disc1 - $disc2) . '"
					ProductName="' . str_replace('"', "'", $prod['name']) . '"
					WarehouseCode=""
					Discount="'.($item['reg_discount']+$item['discount_coup']).'" />';
	  }
	  $transportPrice = round($item['Shippings'][0]['cost']/1.2,2);
	  $transDisc = 0;$tDisc = 0;
	  if ($item['Shippings'][0]['transp_type'] == 'POST24') {
		$transDisc = variable('post24 soodustus');
		$tDisc = round($transportPrice - ($transportPrice * (1 - $transDisc / 100)),2);
	  }
	  $xmlStr .= '<line
				  ProductID="' . $item['Shippings'][0]['transp_type'] . '"
				  Quantity="1"
				  UnitPrice="' . $transportPrice . '"
				  LineTotal="' . ($transportPrice - $tDisc) . '"
				  ProductName="' . $item['Shippings'][0]['information']. '"
				  Discount="'.$transDisc.'"';
	  $xmlStr .= ' />';
	  $xmlStr .= '</order>';
	}
	$xml = '<?xml version="1.0" encoding="UTF-8"?>
	<xml>
	  <orders>
	  ' . $xmlStr . '
	  </orders>
	</xml>';
	return $xml;
  }

  protected function getRequestParams(array $items) {
	//echo $this->getXmlStr($items);exit;
	return array('xml' => $this->getXmlStr($items));
  }

  protected function getRemotePks() {
	$q = Doctrine_Query::create()
					->select('
					p.code as code
				')
					->from('Product p')
					->whereIn('p.id', $this->getPks())
					->setHydrationMode(Doctrine::HYDRATE_SINGLE_SCALAR);
	return $q->execute();
  }

  protected function isSuccessfull($response) {
	
  }

}